//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.controller;

import com.github.pagehelper.PageInfo;
import com.qst.crop.common.Result;
import com.qst.crop.entity.Discuss;
import com.qst.crop.entity.Knowledge;
import com.qst.crop.service.DiscussService;
import com.qst.crop.service.KnowledgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(
        tags = {"知识模块接口"}
)
@RestController
@RequestMapping({"/knowledge"})
@CrossOrigin
public class KnowledgeController {
    @Autowired
    private KnowledgeService knowledgeService;
    @Autowired
    private DiscussService discussService;

    public KnowledgeController() {
    }

    @ApiOperation("分页查询所有知识")
    @GetMapping({"/{pageNum}"})
    public Result<PageInfo<Knowledge>> findPage(@PathVariable Integer pageNum) {
        PageInfo<Knowledge> knowledgePageInfo = this.knowledgeService.findPage(pageNum);
        return new Result(true, 20000, "查询成功", knowledgePageInfo);
    }

    @ApiOperation("添加知识")
    @PostMapping
    @PreAuthorize("hasAuthority('expert')")
    public Result add(@RequestBody Knowledge knowledge) {
        this.knowledgeService.add(knowledge);
        return new Result(true, 20000, "添加成功");
    }

    @ApiOperation("根据id修改知识")
    @PutMapping({"/{id}"})
    public Result update(@RequestBody Knowledge knowledge, @PathVariable("id") Integer id) {
        this.knowledgeService.update(knowledge, id);
        return new Result(true, 20000, "修改成功");
    }

    @ApiOperation("根据id删除知识")
    @DeleteMapping({"/{id}"})
    public Result delete(@PathVariable("id") Integer id) {
        this.knowledgeService.delete(id);
        return new Result(true, 20000, "删除成功");
    }

    @ApiOperation("根据登录用户查询知识")
    @GetMapping({"/selectByUsername"})
    public Result selectByUsername() {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        List<Knowledge> knowledges = this.knowledgeService.selectByUsername(name);
        return new Result(true, 20000, "查询成功", knowledges);
    }

    @ApiOperation("根据id查询知识")
    @GetMapping({"/selectById/{id}"})
    public Result selectById(@PathVariable("id") Integer id) {
        Knowledge knowledge = this.knowledgeService.selectById(id);
        return new Result(true, 20000, "查询成功", knowledge);
    }

    @ApiOperation("查询讨论消息")
    @GetMapping({"/selectByKnowledge/{id}"})
    public Result selectByKnowledge(@PathVariable("id") Integer id) {
        List<Discuss> discuss = this.discussService.selectByKnowledgeId(id);
        return new Result(true, 20000, "查询成功", discuss);
    }

    @ApiOperation("添加讨论消息")
    @PostMapping({"/addByKnowledge/{id}/{content}"})
    public Result addByKnowledge(@PathVariable("id") Integer id, @PathVariable("content") String content) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Discuss discuss = new Discuss();
        discuss.setKnowledgeId(id);
        discuss.setOwnName(name);
        discuss.setCreateTime(new Date());
        discuss.setContent(content);
        this.discussService.add(discuss);
        return new Result(true, 20000, "添加成功", discuss);
    }

    @ApiOperation("分页条件查询所有知识")
    @GetMapping({"/{keys}/{pageNum}"})
    public Result<PageInfo<Knowledge>> findPageByKeys(@PathVariable String keys, @PathVariable Integer pageNum) {
        PageInfo<Knowledge> knowledgePageInfo = this.knowledgeService.findPageByKeys(keys, pageNum);
        return new Result(true, 20000, "查询成功", knowledgePageInfo);
    }
}
